package com.xingkeduo.business.user.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.transformation.json.BaseDTOJTO;
import com.xingkeduo.business.transformation.json.CraftsmanJTO;
import com.xingkeduo.business.transformation.json.CraftsmanServiceJTO;
import com.xingkeduo.business.transformation.json.PriceManagementDtoJTO;
import com.xingkeduo.business.user.domain.ManageInfoDomain;
import com.xingkeduo.business.user.domain.ServiceDomain;
import com.xingkeduo.pojo.dto.BaseDTO;
import com.xingkeduo.pojo.query.CraftsmanQO;
import com.xingkeduo.pojo.query.CraftsmanServiceQO;
import com.xingkeduo.pojo.utils.ResponseUtils;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务配置
 *
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/5/6 15:04
 **/
@Slf4j
@RestController
@RequestMapping("/craftsman/service")
public class CraftsmanServiceController extends BaseController {

    @Autowired
    private ManageInfoDomain manageInfoDomain;
    @Autowired
    private ServiceDomain serviceDomain;

    /**
     * @desc：根据店铺id、服务类型id获取对应的发型师配置的价格
     *  shopId必填 serviceId必填   craftsmanId非必填
     * @author: pantong
     * @date: 2019年5月8日 下午4:25:52
     */
    @GetMapping("/listCraftsmanPriceByShopIdAndServiceId")
    public ResponseUtils listCraftsmanPriceByShopIdAndServiceId(Long shopId, Long serviceId, Long craftsmanId) {
        try {
            CraftsmanServiceQO craftsmanServiceQO = new CraftsmanServiceQO();

            if (shopId != null) {
                craftsmanServiceQO.setShopId(shopId);
            }else {
            	return ResponseUtils.error("error", "店铺id必填");
            }
            if (serviceId != null) {
                craftsmanServiceQO.setServiceId(serviceId);
            }else {
            	return ResponseUtils.error("error", "skuid必填");
            }
            if (craftsmanId != null) {
                craftsmanServiceQO.setCraftsmanId(craftsmanId);
            }
            craftsmanServiceQO.setIsDelete(YesNoEnum.NO);
            craftsmanServiceQO.setStatus(YesNoEnum.NO);
            return ResponseUtils.getSuccess(serviceDomain.listCraftsmanPriceByShopIdAndServiceId(craftsmanServiceQO));
        } catch (Throwable e) {
            e.printStackTrace();
            return ResponseUtils.error("error", "");
        }
    }

    /**
     * 获取店铺下提供某个服务的发型师列表信息（含价格 价格取该店铺发型师配置的价格 如果没有配置返回空即可）pantong  2019年6月1日下午5:47:03
     * @param shopId 必填
     * @param serviceId 必填
     * @param craftsmanId 必填
     * @return
     * ResponseUtils
     */
    @GetMapping("/listCraftsmanInfoByShopIdAndServiceId")
    public ResponseUtils listCraftsmanInfoByShopIdAndServiceId(Long shopId, Long serviceId, String  craftsmanIds) {
        try {
            if (shopId == null ||  shopId.longValue()<1L) {
            	return ResponseUtils.error("error", "店铺id必填且值要大于0");
            }
            if (serviceId == null) {
            	return ResponseUtils.error("error", "serviceId必填");
            } 
            if (StringUtils.isEmpty(craftsmanIds)) {
            	return ResponseUtils.error("error", "craftsmanIds必填");
            }
            return ResponseUtils.getSuccess(serviceDomain.listCraftsmanInfoByShopIdAndServiceId(shopId, serviceId, craftsmanIds));
        } catch (Throwable e) {
            e.printStackTrace();
            return ResponseUtils.error("error", "");
        }
    }



    /**
     * 查询发型师服务技能价格
     *
     * @param :
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/16 10:03
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable String id) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO queryForPrimary = CraftsmanJTO.createQueryForPrimary(id);
            return returnUtil.getSuccess(PriceManagementDtoJTO.toJsonDetail(serviceDomain.craftsmanServiceDetail(queryForPrimary)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 服务技能新增修改
     *
     * @param serviceid:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/6 15:06
     */
    @PostMapping("/update/{id}")
    public String updateCraftsmanService(@PathVariable String id, @RequestBody String serviceid) {
        returnUtil = new ReturnUtil();
        if (StringUtils.isEmpty(serviceid)) {
            return returnUtil.getException("0", "服务不能为空");
        }
        try {
            CraftsmanQO craftsmanQO = CraftsmanJTO.createQueryForPrimary(id);
            CraftsmanServiceQO queryForList = CraftsmanServiceJTO.createQueryForList(serviceid);
            serviceDomain.craftsmanUpdateService(craftsmanQO, queryForList);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 查询发型师服务技能 返回name
     *
     * @param id:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/6 15:23
     */
    @GetMapping("/check/{id}")
    public String getCraftsmanServiceInfo(@PathVariable String id) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO craftsmanQO = CraftsmanJTO.createQueryForPrimary(id);
            return returnUtil.getSuccess(BaseDTOJTO.toJson(serviceDomain.craftsmangetService(craftsmanQO)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 查询店铺和发型师服务的交集
     *
     * @param body: 传发型师id 和门店id
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/17 10:16
     */
    @PostMapping("/price/mixed")
    public String getPriceMixed(@RequestBody String body) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanServiceQO queryList = CraftsmanServiceJTO.createQueryList(body);
            return returnUtil.getSuccess(BaseDTOJTO.toJson(serviceDomain.getPriceMixed(queryList)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 编辑店铺和发型师服务的交集
     *
     * @param body: 传发型师id 和门店id
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/17 10:16
     */
    @PostMapping("/updateprice/mixed")
    public String updateGetPriceMixed(@RequestBody String body) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanServiceQO queryList = CraftsmanServiceJTO.createQueryList(body);
            return returnUtil.getSuccess(BaseDTOJTO.toJson(serviceDomain.updateGetPriceMixed(queryList)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }


    /**
     * 新增调价
     *
     * @param id:
     * @param body:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/6 20:54
     */
    @PostMapping("/price/insert/{id}")
    public String insertPrice(@PathVariable String id, @RequestBody String body) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO primary = CraftsmanJTO.createQueryForPrimary(id);
            CraftsmanServiceQO queryForList = CraftsmanServiceJTO.createQueryForList(body);
            if (queryForList.getShopId() <= 0) {
                throw new DataException(DataException.Type.DEFAULT, "门店不能为空");
            }
            serviceDomain.insertPrice(primary, queryForList);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }
    /**
     * 编辑调价
     *
     * @param id:
     * @param body:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/6 20:54
     */
    @PostMapping("/price/update/{id}")
    public String updatePrice(@PathVariable String id, @RequestBody String body) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO primary = CraftsmanJTO.createQueryForPrimary(id);
            CraftsmanServiceQO queryForList = CraftsmanServiceJTO.createQueryForList(body);
            if (queryForList.getShopId() <= 0) {
                throw new DataException(DataException.Type.DEFAULT, "门店不能为空");
            }
            serviceDomain.updatePrice(primary, queryForList);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 发型师门店价格失效
     *
     * @param :
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/7 15:55
     */
    @GetMapping("/out/{body}")
    public String craftsmanServiceOut(@PathVariable String body) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanQO primary = CraftsmanJTO.createQueryForPrimary(body);
            CraftsmanServiceQO queryForList = CraftsmanServiceJTO.createQueryForList(body);
            if (queryForList.getShopId() <= 0) {
                throw new DataException(DataException.Type.DEFAULT, "门店不能为空");
            }
            serviceDomain.craftsmanServiceOutOrEff(primary, queryForList);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }

    }


    /**
     * 根据店铺id和发型师集合 查询发型师服务技能和价格
     *
     * @param shopId:
     * @param craftsmanIds:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/29 16:30
     */
    @PostMapping("/shopService/list/{shopId}")
    public String getCraftsmanServiceList(@PathVariable String shopId, @RequestBody List<Long> craftsmanIds) {
        returnUtil = new ReturnUtil();
        try {
            log.info("shopService传来的店铺id" + shopId);
            CraftsmanServiceQO shopQO = CraftsmanServiceJTO.createQueryForList(shopId);
            log.info("shopService传来的店铺信息" + shopQO.toString());
            return returnUtil.getSuccess(BaseDTOJTO.toJson(manageInfoDomain.getCraftsmanServiceList(shopQO,craftsmanIds)));
        } catch (Throwable t) {
            log.info("shop传过来的数据失败" + t.toString());
            return dealException(t, returnUtil);
        }
    }


    /**
     * 查询发型师服务
     *
     * @param craftsmanIds: 传发型师id 和门店id
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/17 10:16
     */
    @PostMapping("/serviceids")
    public ResponseUtils<List<BaseDTO>> getCraftsmanServiceForList(@RequestBody List<Long> craftsmanIds) {
        log.info("查询多个，发型师服务技能集合"+craftsmanIds.size());
        try {
            List<BaseDTO> craftsmanServiceForList = serviceDomain.getCraftsmanServiceForList(craftsmanIds);
            return ResponseUtils.getSuccess(craftsmanServiceForList);
        } catch (Throwable t) {
            return ResponseUtils.error(t.getMessage(), "-1");
        }
    }


    /**店铺更改技能 替换发型师所有服务技能
     * @param shopId:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/6/6 10:30
     */
    @PostMapping("/shop/update")
    public String shopUpdateService(@RequestBody String body){
        returnUtil = new ReturnUtil();
        try {
            log.info("门店更换需要替换发型师的服务技能---shopId" + body);
            CraftsmanServiceQO shopQO = CraftsmanServiceJTO.createQueryForList(body);
            log.info("门店更换需要替换发型师的服务技能 -=----传来的店铺信息" + JSON.toJSONString(shopQO));
            manageInfoDomain.shopUpdateService(shopQO);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            log.info("shop传过来的数据失败" + t.toString());
            return dealException(t, returnUtil);
        }
    }
}
